//
//  MBStatisticsMonthPickerView.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/24.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBStatisticsMonthPickerView.h"

@interface MBStatisticsMonthPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic,weak) UIView * backShadowView;
@property (nonatomic,weak) UIView * containView;

@property (nonatomic,strong)UIPickerView* pickerView;

@property (nonatomic,assign)NSInteger maxYear;
@property (nonatomic,assign)NSInteger maxMonthy;
@end

@implementation MBStatisticsMonthPickerView

-(instancetype)init
{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self = [super init]) {
        //创建shadowView
        UIView * backShadowView = [[UIView alloc] init];
        backShadowView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
        backShadowView.alpha = 0.0;
        [self addSubview:backShadowView];
        self.backShadowView = backShadowView;
        
        [backShadowView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)]];
        
        [backShadowView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self).insets(UIEdgeInsetsZero);
        }];
        
        
        UIView * containView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, [UIView lf_sizeFromIphone6:222])];
        containView.backgroundColor = [UIColor whiteColor];
        [self addSubview:containView];
        self.containView = containView;

        UIButton* cancelButton = [[UIButton alloc]init];
        [cancelButton addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancelButton setTitleColor:[UIColor colorWithR:24 G:24 B:24] forState:UIControlStateNormal];
        cancelButton.titleLabel.font = [UIFont lf_systemFontOfSize:14];
        [containView addSubview:cancelButton];
        
        [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(containView).offset([UIView lf_sizeFromIphone6:12]);
            make.top.equalTo(containView).offset([UIView lf_sizeFromIphone6:12]);
            make.height.mas_equalTo(20);
        }];
        
        UIButton* shureButton = [[UIButton alloc]init];
        self.shureButton = shureButton;
        [shureButton setTitleColor:COLOR_MAIN forState:UIControlStateNormal];
        shureButton.titleLabel.font = [UIFont lf_systemFontOfSize:14];
        [shureButton setTitle:@"确定" forState:UIControlStateNormal];
        [containView addSubview:shureButton];
        
        [shureButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(containView).offset([UIView lf_sizeFromIphone6:-12]);
            make.top.equalTo(containView).offset([UIView lf_sizeFromIphone6:12]);
            make.height.mas_equalTo(20);
        }];

        UIPickerView* pickerView = [[UIPickerView alloc] init];
        pickerView.backgroundColor = [UIColor whiteColor];
        self.pickerView = pickerView;
        pickerView.delegate = self;
        pickerView.dataSource = self;
        NSDate *now = [NSDate date];
        NSCalendar *cal = [NSCalendar currentCalendar];
        unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
        NSDateComponents *dd = [cal components:unitFlags fromDate:now];
        NSInteger year = [dd year];
        NSInteger month = [dd month];
        self.maxYear = year;
        self.maxMonthy = month;
        self.year = year;
        self.monthy = month;
        [containView addSubview:pickerView];
        
        [pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(shureButton.mas_bottom).offset([UIView lf_sizeFromIphone6:5]);
            make.left.bottom.right.equalTo(containView);
        }];
        
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        [[UIApplication sharedApplication].keyWindow sendSubviewToBack:self];

    }
    return self;
}


- (void)show
{
    [self.pickerView selectRow:(self.year - 2017) inComponent:0 animated:YES];
    [self.pickerView selectRow:self.monthy -1 inComponent:1 animated:YES];
    [self.pickerView reloadComponent:1];

    
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.backShadowView.alpha = 1.0;
        self.containView.y = SCREEN_HEIGHT - self.containView.height;
    }];
}

- (void)close
{
    [self endEditing:YES];
    [UIView animateWithDuration:0.3 animations:^{
        self.backShadowView.alpha = 0.0;
        self.containView.y = SCREEN_HEIGHT;
    } completion:^(BOOL finished) {
        [[UIApplication sharedApplication].keyWindow sendSubviewToBack:self];
    }];
}

-(void)setYear:(NSInteger)year
{
    _year = year;
    [self.pickerView selectRow:(self.year - 2017) inComponent:0 animated:YES];
    [self.pickerView reloadComponent:1];
}

-(void)setMonthy:(NSInteger)monthy
{
    _monthy = monthy;
    [self.pickerView selectRow:self.monthy -1 inComponent:1 animated:YES];
}

#pragma mark UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    NSInteger selectYear;
    switch (component) {
        case 0:
            return self.maxYear - 2017+1;
            break;
        case 1:
           selectYear = [pickerView selectedRowInComponent:0] + 2017;
            if (selectYear == self.maxYear) {
                return self.maxMonthy;
            }else {
                return 12;
            }
        default:
            return 0;
            break;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    switch (component) {
        case 0:
            return [NSString stringWithFormat:@"%ld年", (long)row + 2017];
            break;
        case 1:
            return [NSString stringWithFormat:@"%ld月", (long)(row + 1)];
        default:
            return @"";
            break;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    switch (component) {
        case 0:
            self.year = row +2017;
            [pickerView reloadComponent:1];
            break;
        case 1:
            self.monthy = row +1;
            break;
        default:
            break;
    }
}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.minimumScaleFactor = 8.;
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont lf_systemFontOfSize:18]];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}


@end
